package siau.android.base

import android.content.Context
import android.util.TypedValue
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import hhm.android.library.utils.OnMultiClickListener
import siau.android.http.model.ChildModel
import siau.android.library.KeyString

class BottomSingleBabySelectSheet(context: Context,/* var changeBoolean: Boolean,*/ var pageType: Int, var childId: Int, l: ArrayList<ChildModel>, var listener: GetChildIdListener) : BottomSheetDialog(context) {
    //changeBoolean判断是不是开始测量按钮 pageType和childId判断是不是当前孩子
    lateinit var adapter: BottomSingleBabySelectAdapter
    var list = ArrayList<ChildModel>()
    private var selectChild: Int = childId

    init {
        l.forEachIndexed { index, it ->
            if (it.childId == -1 && it.familyId == -1 && it.nickName == "") {
            } else {
                val n = ChildModel(
                    it.childId, it.nickName, if (it.avatar.isNullOrEmpty()) {
                        ""
                    } else {
                        it.avatar
                    }, if (pageType == 3) {
                        index == 0
                    } else {
                        it.select
                    }, it.familyId, it.birthday, it.sex, if (it.height.isNaN()) {
                        -1.0
                    } else {
                        it.height
                    }, if (it.weight.isNaN()) {
                        -1.0
                    } else {
                        it.weight
                    }
                )
                list.add(n)
            }
        }
        createView()
    }

    private fun createView() {
        val bottomSheetView: View = layoutInflater.inflate(R.layout.item_bottom_select_baby, null)
        setContentView(bottomSheetView)

        val tv: TextView = bottomSheetView.findViewById(R.id.item_bottom_select_baby_top_tv)
        val recycler: RecyclerView = bottomSheetView.findViewById(R.id.item_bottom_select_baby_recycler)
        val sure: TextView = bottomSheetView.findViewById(R.id.item_bottom_select_baby_sure)
        val cancel: TextView = bottomSheetView.findViewById(R.id.item_bottom_select_baby_cancel)
        val addBaby: TextView = bottomSheetView.findViewById(R.id.item_bottom_select_baby_add)
        if (SBApplication.userData.isCreator != 1) {
            addBaby.visibility = View.GONE
        }
        var name = ""
        list.forEach {
            if (it.childId == selectChild) {
                name = it.nickName
            }
        }
        if (pageType == 3) {
            tv.text = context.getString(R.string.please_select_match_toothbrush_baby)
        } else {
            tv.text = context.getString(R.string.item_bottom_select_baby_tip1) + name + context.getString(R.string.item_bottom_select_baby_tip2)
        }

        recycler.layoutManager = LinearLayoutManager(context)

        adapter = BottomSingleBabySelectAdapter(context, list, pageType, object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                val pos = v?.tag as Int
                list.forEach {
                    it.select = false
                }
                list[pos].select = true
                selectChild = list[pos].childId
                if (pageType == 3) {

                } else {
                    tv.text = context.getString(R.string.item_bottom_select_baby_tip1) + list[pos].nickName + context.getString(R.string.item_bottom_select_baby_tip2)
                }
                adapter.notifyDataSetChanged()
            }
        })
        recycler.adapter = adapter

        cancel.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
//                if (changeBoolean && SBApplication.calculateType == pageType && SBApplication.calculateChild == childId) {
//                    SBApplication.startCalculate.value = false
//                }
                dismiss()
            }
        })
        sure.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                listener.getChildId(selectChild)
                dismiss()
            }
        })
        addBaby.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                ARouter.getInstance().build(BABY_ROUTE).withInt(KeyString.type, 0).navigation()
//                if (changeBoolean && SBApplication.calculateType == pageType && SBApplication.calculateChild == childId) {
//                    SBApplication.startCalculate.value = false
//                }
                dismiss()
            }
        })

        // 注意：这里要给layout的parent设置peekHeight，而不是在layout里给layout本身设置，下面设置背景色同理，坑爹！！！
        val bottomSheetBehavior: BottomSheetBehavior<*> = BottomSheetBehavior.from(
            bottomSheetView.parent as View
        )
        bottomSheetView.measure(0, 0)
        behavior.peekHeight = bottomSheetView.measuredHeight

        bottomSheetBehavior.peekHeight = bottomSheetView.measuredHeight
        //允许点击外部关闭，不允许滑动关闭
        bottomSheetBehavior.isHideable = false
        (bottomSheetView.parent as View).setBackgroundColor(context.resources.getColor(R.color.trans))
    }

    fun dp2px(dpValue: Float?): Int {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, dpValue!!, context.resources.displayMetrics
        ).toInt()
    }
}